package com.lyl.momanapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lyl.momanapp.R;
import com.lyl.momanapp.bean.User;

import java.util.ArrayList;

/**
 * 名字 梁亚龙
 * 时间 2017/9/10.
 * QQ邮箱 503934353@qq.com
 */

public class AddressesAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private ArrayList<User.listAddress.ResultBean> arrayList;
    private int num;

    public interface OnItemClickLitener {
        void onCheckBoxClick(View view, int position);
        void onItemClick(View view, int position);
        void ondelClick(View view, int position);
        void onxiugaiClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public AddressesAdapter(Context context, ArrayList<User.listAddress.ResultBean> arrayList) {
        this.mInflater = LayoutInflater.from(context);
        this.arrayList =arrayList;
    }
    public void  adddata(ArrayList<User.listAddress.ResultBean> arrayList){
        if (this.arrayList!=null) {
            this.arrayList.clear();
        }
        num=0;
        this.arrayList.addAll(arrayList);
        this.notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        if (arrayList==null){
            return 0;
        }else {
            return arrayList.size();
        }
    }

    @Override
    public Object getItem(int i) {
        return arrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = new ViewHolder();
        if (num == i) {
            view = mInflater.inflate(R.layout.item_address_moren, null);
            viewHolder.name = view.findViewById(R.id.item_address_moren_name);
            viewHolder.phone = view.findViewById(R.id.item_address_moren_phone);
            viewHolder.del = view.findViewById(R.id.item_address_moren_del);
            viewHolder.xiugai = view.findViewById(R.id.item_address_moren_xiugai);
            viewHolder.address = view.findViewById(R.id.item_address_moren_add);
            viewHolder.checkbox = view.findViewById(R.id.item_address_moren_checkbox);
            viewHolder.ll =view.findViewById(R.id.item_address_moren_ll);
        } else {
            view = mInflater.inflate(R.layout.item_address_yiban, null);
            viewHolder.name = view.findViewById(R.id.item_address_yiban_name);
            viewHolder.phone = view.findViewById(R.id.item_address_yiban_phone);
            viewHolder.del = view.findViewById(R.id.item_address_yiban_del);
            viewHolder.xiugai = view.findViewById(R.id.item_address_yiban_xiugai);
            viewHolder.address = view.findViewById(R.id.item_address_yiban_add);
            viewHolder.checkbox = view.findViewById(R.id.item_address_yiban_checkbox);
            viewHolder.ll =view.findViewById(R.id.item_address_yiban_ll);
        }
        viewHolder.name.setText(arrayList.get(i).getConsignee());
        viewHolder.phone.setText(arrayList.get(i).getPhone());
        viewHolder.address.setText(arrayList.get(i).getComplete_city_info());

        itemOnClick(viewHolder,i);


        return view;
    }

    private void itemOnClick(ViewHolder viewHolder, final int i) {
        viewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num = i;
                mOnItemClickLitener.onCheckBoxClick(view, i);
                AddressesAdapter.this.notifyDataSetChanged();
            }
        });
        viewHolder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickLitener.ondelClick(view,i);
            }
        });
        viewHolder.xiugai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickLitener.onxiugaiClick(view,i);
            }
        });
        viewHolder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickLitener.onItemClick(view,i);
            }
        });
    }

    class ViewHolder {
        private LinearLayout ll;
        private TextView name, phone, del, xiugai, address;
        private ImageView checkbox;
    }
}
